package com.hdax.controller;

import com.hdax.entity.Permission;
import com.hdax.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限控制器
 *
 * @author Aizen
 * @date 2022/05/10
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    /**
     * 改变
     *
     * @param rid 掉
     * @return {@link ModelAndView}
     */
    @GetMapping("/change")
    public ModelAndView change(@RequestParam("rid") Integer rid){
        ModelAndView modelAndView = new ModelAndView("system/shuxing");
        modelAndView.addObject("rid",rid);
        return modelAndView;
    }

    /**
     * 改变权限
     *
     * @param rid 角色ID
     * @param ids 权限ID集合
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @ResponseBody
    @PostMapping(path = "/change")
    public Map<String,Object> change(@RequestParam("rid") Integer rid, @RequestParam("ids[]") List<Integer> ids){
        //"复合"方法： 删除+添加  方法保证一致性
        permissionService.changePermission(rid, ids);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("msg","权限修改成功！");
        return result;
    }

    /**
     * 权限
     *
     * @param rid 掉
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @PostMapping(path = "/role/{rid}")
    @ResponseBody
    public Map<String,Object> permissions(@PathVariable(value = "rid",required = false)Integer rid){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> status = new HashMap<>();
        status.put("code",200);
        status.put("message","success");
        map.put("status",status);
        List<Permission> list = permissionService.allPermissions(rid);
        map.put("data",list);


        return map;
    }





}
